﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Aierp.Entities;
using Aierp.SystemEntities.System;
using Aierp.Utility;
using Microsoft.AspNetCore.Http;

namespace Aierp.Utility.Core
{
    public class SysVisitorCustomer
    {
        #region Session Key

        public const string SessionUserIdKey = "AIERP-CUP-CUSTOMER-USERID";
        public const string SessionUserNameKey = "AIERP-CUP-CUSTOMER-USERNAME";
        public const string SessionIsAdminKey = "AIERP-CUP-CUSTOMER-ISADMIN";
        #endregion

        #region CookieName Key

        public const string CookieNameKey = "AIERP-CUP-COOKIE-CUSTOMER-NAME";
        public const string CookieUserNameKey = "AIERP-CUP-COOKIE--CUSTOMER-USERNAME";
        public const string CookiePasswordKey = "AIERP-CUP-COOKIE--CUSTOMER-PASSWORD";
        #endregion

        private readonly IHttpContextAccessor _httpContextAccessor;
        public ISession Session => _httpContextAccessor.HttpContext.Session;
        public SysVisitorCustomer(IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }
        /// <summary>
        /// 用户ID
        /// </summary>
        public int UserId
        {
            get { return (Session.GetInt32(SessionUserIdKey)) ?? 0; }
            set { Session.SetInt32(SessionUserIdKey, value); }
        }

        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName
        {
            get
            {
                return (Session.GetString(SessionUserNameKey));
            }
            set
            {
                Session.SetString(SessionUserNameKey, value);
            }
        }

        /// <summary>
        /// 当前用户
        /// </summary>
        public C1_Customer CurrentCustomer
        {
            get { return JSONhelper.ConvertToObject<C1_Customer>(Session.GetString("AIERP-CUP-CUSTOMER")); }
            set { Session.SetString("AIERP-CUP-CUSTOMER", JSONhelper.ToJson(value)); }
        }
    }
}
